# Kernel config based on: arch/arm/configs/jenad_defconfig

pkgname="linux-samsung-s6500d"
pkgver=3.4.105
pkgrel=0
pkgdesc="Samsung Galaxy Mini 2 kernel fork"
arch="armhf"
_carch="arm"
_flavor="samsung-s6500d"
url="https://github.com/Shadowofleaf/android_kernel_samsung_msm7x27a/"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_samsung_msm7x27a"
_commit="d70eebdb55d7011c82ce64fe5eae2fdcbfd4e425"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/Shadowofleaf/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
	00_return_address.patch
	02_gpu-msm-fix-gcc5-compile.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$builddir/include/linux/"

	# Remove -Werror from all makefiles
	find . -type f -name Makefile -print0 | \
		xargs -0 sed -i 's/-Werror-/-W/g'
	find . -type f -name Makefile -print0 | \
		xargs -0 sed -i 's/-Werror//g'

	# Prepare kernel config ('yes ""' for kernels lacking olddefconfig)
	cp "$srcdir"/$_config "$builddir"/.config
	yes "" | make ARCH="$_carch" HOSTCC="$HOSTCC" oldconfig
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
}

package() {
	# kernel.release
	install -D "$builddir/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	# zImage (find the right one)
	cd "$builddir/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in zImage-dtb Image.gz-dtb *zImage Image; do
		[ -e "$_zimg" ] || continue
		msg "zImage found: $_zimg"
		install -Dm644 "$_zimg" "$_target"
		break
	done
	if ! [ -e "$_target" ]; then
		error "Could not find zImage in $PWD!"
		return 1
	fi
}

sha512sums="a46d52f79597fc3a8ded036fbdcc4cdbb96ba1582fb1349002183c1e23598da10da2faede1a251f55705ae6e8e54232797866f4c03acc2b1ace1664b315dd216  linux-samsung-s6500d-d70eebdb55d7011c82ce64fe5eae2fdcbfd4e425.tar.gz
41f5709398318f4d26b82ea7c5a58bce2b733d92cfc88838926658be602ff379567f41690f27ce47830983efd9508ccc149c8c80583de5a366d6d3d6cab36cd7  config-samsung-s6500d.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
c40eaa11547f5bc2b1ff965506bdfe3015ff16e4b5ad9ccb3b8134ceafd1d32407c4ef2b213e02e036ce1e9bdcbfe768dff7d4b054bec77705a176c1f783b6f5  00_return_address.patch
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch"
